/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.jefb.annotations.LazyInit;
import org.jefb.sec.entity.UserRight;
import org.springframework.beans.factory.annotation.Configurable;

@Entity(name="user")
@Table(name="CV_USER")
@Configurable
public class UserEntity extends BasicEntity implements Serializable, Comparable<UserEntity>{
	
	private static final long serialVersionUID = 1L;
	
	private String username;
	private String password;
	private String firstName;
	private String lastName;
	private String homeDir;
	
	private boolean disabled = false;
	private boolean deleted = false;
	
	private Collection<RoleEntity> userRoles;
	private Collection<LinkEntity> shortcuts;
	
	private Collection<UserRight> userRights = new ArrayList<UserRight>();
	
	@Column(name="USERNAME", nullable=false, unique=true)
	public String getUsername() {return username;}
	
	@Column(name="PASSWORD", nullable=false)
	public String getPassword() {return password;}
	
	@Column(name="FIRST_NAME")
	public String getFirstName() {return firstName;}
	
	@Column(name="LAST_NAME")
	public String getLastName() {
		return lastName;
	}
	
	@Column(name="HOME_DIR", nullable=false)
	public String getHomeDir() {
		return homeDir;
	}
	
	public static final String LAZY_MARKER_ROLES = "userRoles";
	@ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinTable(name="CV_USER_ROLE",
				joinColumns={@JoinColumn(name="USER_ID")}, 
				inverseJoinColumns={@JoinColumn(name="ROLE_ID")})
	@LazyInit(LAZY_MARKER_ROLES)
	public Collection<RoleEntity> getUserRoles() {return userRoles;}
	
	@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinTable(name="CV_USER_SHORTCUT",
				joinColumns={@JoinColumn(name="USER_ID")}, 
				inverseJoinColumns={@JoinColumn(name="SHORTCUT_ID")})
	public Collection<LinkEntity> getShortcuts() {return shortcuts;}
	
	@ElementCollection(fetch=FetchType.EAGER, targetClass=UserRight.class)
	@CollectionTable(name="CV_USER_RIGHT",joinColumns=@JoinColumn(name="USER_ID"))
	public Collection<UserRight> getUserRights() {
		return userRights;
	}
	
	@Column(name="DELETED")
	public boolean isDeleted() {return deleted;}
	
	@Column(name="DISABLED")
	public boolean isDisabled() {return disabled;}
	
	public void setUsername(String username) {this.username = username;}
	public void setPassword(String password) {this.password = password;}
	public void setFirstName(String firstName) {this.firstName = firstName;}
	public void setLastName(String lastName) {this.lastName = lastName;}
	
	public void setUserRoles(Collection<RoleEntity> userRoles) {this.userRoles = userRoles;}

	public int compareTo(UserEntity arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setShortcuts(Collection<LinkEntity> shortcuts) {
		this.shortcuts = shortcuts;
	}

	public void setHomeDir(String homeDir) {
		this.homeDir = homeDir;
	}

	public void setUserRights(Collection<UserRight> userRights) {
		this.userRights = userRights;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}
	
}
